package com.assets.Scrap;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.Nullable;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.assets.newNHF.util.LogUtils;
import com.assets.util.TrustAllCerts;
import com.bigkoo.alertview.OnDismissListener;
import com.assets.Ip_set.Url;
import com.assets.R;
import com.bumptech.glide.Glide;
import com.hb.dialog.dialog.LoadingDialog;

import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2019/5/11.
 */

public class Scrap_ck extends Activity implements OnDismissListener {
    private String repairsId, number, states;//单号,编号
    private Context mContext;
    private List<String> imageS = new ArrayList<>();
    private ImageView fh, ImageView_1, ImageView_2, ImageView_3, ImageView_4, ImageView_5;
    private Button uploading;
    private Handler handler = new Handler();
    private ImageView add_video;
    private String url = "http://" + Url.IP + "/api/scrapImageQuery";
    private ScrollView scroll;
    LoadingDialog loadingDialog;
    private Dialog dialog;
    private TextView remark;
    private TextView Section, Asset_name, Model, title_text;//部门，名称，型号，备注


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scrap_ck);
        mContext = this;
        inview();
        ImageView_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = new Dialog(Scrap_ck.this, R.style.FullActivity);
                WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes();
                attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
                attributes.height = WindowManager.LayoutParams.MATCH_PARENT;
                dialog.getWindow().setAttributes(attributes);


                /*ImageView_1.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(ImageView_1.getDrawingCache());
                ImageView_1.setDrawingCacheEnabled(false);*/
                ImageView imageView = new ImageView(Scrap_ck.this);
                imageView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                Glide.with(Scrap_ck.this).load(imageS.get(0)).into(imageView);
                dialog.setContentView(imageView);
                //大图的点击事件（点击让他消失）
                dialog.show();
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
        });
        ImageView_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = new Dialog(Scrap_ck.this, R.style.FullActivity);
                WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes();
                attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
                attributes.height = WindowManager.LayoutParams.MATCH_PARENT;
                dialog.getWindow().setAttributes(attributes);

               /* ImageView_2.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(ImageView_2.getDrawingCache());
                ImageView_2.setDrawingCacheEnabled(false);*/
                ImageView imageView = new ImageView(Scrap_ck.this);
                imageView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                Glide.with(Scrap_ck.this).load(imageS.get(1)).into(imageView);
                dialog.setContentView(imageView);
                //大图的点击事件（点击让他消失）
                dialog.show();
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
        });
        ImageView_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = new Dialog(Scrap_ck.this, R.style.FullActivity);
                WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes();
                attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
                attributes.height = WindowManager.LayoutParams.MATCH_PARENT;
                dialog.getWindow().setAttributes(attributes);

               /* ImageView_3.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(ImageView_3.getDrawingCache());
                ImageView_3.setDrawingCacheEnabled(false);*/
                ImageView imageView = new ImageView(Scrap_ck.this);
                imageView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                Glide.with(Scrap_ck.this).load(imageS.get(2)).into(imageView);
                dialog.setContentView(imageView);
                //大图的点击事件（点击让他消失）
                dialog.show();
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
        });
        ImageView_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = new Dialog(Scrap_ck.this, R.style.FullActivity);
                WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes();
                attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
                attributes.height = WindowManager.LayoutParams.MATCH_PARENT;
                dialog.getWindow().setAttributes(attributes);

               /* ImageView_4.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(ImageView_4.getDrawingCache());
                ImageView_4.setDrawingCacheEnabled(false);*/
                ImageView imageView = new ImageView(Scrap_ck.this);
                imageView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                Glide.with(Scrap_ck.this).load(imageS.get(3)).into(imageView);
                dialog.setContentView(imageView);
                //大图的点击事件（点击让他消失）
                dialog.show();
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
        });
        ImageView_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = new Dialog(Scrap_ck.this, R.style.FullActivity);
                WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes();
                attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
                attributes.height = WindowManager.LayoutParams.MATCH_PARENT;
                dialog.getWindow().setAttributes(attributes);

              /*  ImageView_5.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(ImageView_5.getDrawingCache());
                ImageView_5.setDrawingCacheEnabled(false);*/
                ImageView imageView = new ImageView(Scrap_ck.this);
                imageView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                Glide.with(Scrap_ck.this).load(imageS.get(4)).into(imageView);
                dialog.setContentView(imageView);
                //大图的点击事件（点击让他消失）
                dialog.show();
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

            }
        });

    }

    private void load() {
        new Thread(() -> {
            OkHttpClient client =new OkHttpClient
                .Builder()
                .hostnameVerifier(new AllowAllHostnameVerifier())
                .sslSocketFactory(TrustAllCerts.createSSLSocketFactory(), TrustAllCerts.getX509TrustManager())
                .connectTimeout(10, TimeUnit.SECONDS)//连接超时
                .readTimeout(500, TimeUnit.SECONDS)
                .build();//创建OkHttpClient对象。
            FormBody.Builder formBody = new FormBody.Builder();//创建表单请求体
            formBody.add("scrapId", repairsId)
                    .add("assetNumber", number); //传递键值对参数
            Request request = new Request.Builder()//创建Request 对象。
                    .url(url)
                    .post(formBody.build())//传递请求体
                    .build();
            LogUtils.d(repairsId + "-" + number + "-" + url);
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    //回调方法的使用与get异步请求相同
                    Log.i("123123", "error");
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    final String responseData = response.body().string();
                    LogUtils.d(responseData);
                    //回调方法的使用与get异步请求相同
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject json = new JSONObject(responseData);
                                if (json.getString("state").equals("1")) {
                                    JSONArray jsonArray = json.getJSONArray("images");
                                    Log.i("123123json", String.valueOf(jsonArray.length()));
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        String image = jsonArray.get(i).toString();
                                        if (!TextUtils.isEmpty(image)) {
                                            image = "http://" + Url.IP  + image;
                                        }
                                        LogUtils.d(image);
                                        imageS.add(image);
                                        if (i == 0) {
                                            ImageView_1.setVisibility(View.VISIBLE);
                                            Glide.with(Scrap_ck.this).load(image).into(ImageView_1);
                                        }
                                        if (i == 1) {
                                            ImageView_2.setVisibility(View.VISIBLE);
                                            Glide.with(Scrap_ck.this).load(image).into(ImageView_2);
                                        }
                                        if (i == 2) {
                                            ImageView_3.setVisibility(View.VISIBLE);
                                            Glide.with(Scrap_ck.this).load(image).into(ImageView_3);
                                        }
                                        if (i == 3) {
                                            ImageView_4.setVisibility(View.VISIBLE);
                                            Glide.with(Scrap_ck.this).load(image).into(ImageView_4);
                                        }
                                        if (i == 4) {
                                            ImageView_5.setVisibility(View.VISIBLE);
                                            Glide.with(Scrap_ck.this).load(image).into(ImageView_5);
                                        }
                                    }
                                    loadingDialog.dismiss();
                                }
                                if (json.getString("state").equals("0")) {
                                    loadingDialog.dismiss();
                                    Toast.makeText(Scrap_ck.this, "没有附件", Toast.LENGTH_LONG).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });

                }
            });

        }).start();
    }

    private void inview() {
        ImageView_1 = (ImageView) findViewById(R.id.ImageView_1);
        ImageView_2 = (ImageView) findViewById(R.id.ImageView_2);
        ImageView_3 = (ImageView) findViewById(R.id.ImageView_3);
        ImageView_4 = (ImageView) findViewById(R.id.ImageView_4);
        ImageView_5 = (ImageView) findViewById(R.id.ImageView_5);
        ImageView_1.setVisibility(View.GONE);
        ImageView_2.setVisibility(View.GONE);
        ImageView_3.setVisibility(View.GONE);
        ImageView_4.setVisibility(View.GONE);
        ImageView_5.setVisibility(View.GONE);

        add_video = (ImageView) findViewById(R.id.add_mv);
        uploading = (Button) findViewById(R.id.uploading);
        title_text = (TextView) findViewById(R.id.title_text);
        title_text.setText("资产详情");
        fh = (ImageView) findViewById(R.id.fh);
        scroll = (ScrollView) findViewById(R.id.scroll);
        remark = (TextView) findViewById(R.id.remark);
        Section = (TextView) findViewById(R.id.maintenance);
        Asset_name = (TextView) findViewById(R.id.name);
        Model = (TextView) findViewById(R.id.model);
        Intent intent = getIntent();
        repairsId = intent.getStringExtra("repairsId");
        number = intent.getStringExtra("number");
        remark.setText(intent.getStringExtra("remark"));
        Section.setText(intent.getStringExtra("Section"));
        Asset_name.setText(intent.getStringExtra("Asset_name"));
        Model.setText(intent.getStringExtra("Model"));
        fh.setOnClickListener(v -> finish());
        loadingDialog = new LoadingDialog(this);
        loadingDialog.setMessage("加载中....");
        loadingDialog.show();
        load();
    }

    @Override
    public void onDismiss(Object o) {

    }


}
